import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useRef } from "react";
import classes from "./chainSwitch.module.less";
import { useDispatch, useSelector } from "react-redux";
import { useLocalStorageSetState } from "../../hooks";
import classNames from "classnames";
import { jsx as _jsx } from "react/jsx-runtime";
function ChainSwitch(props) {
    var theme = useSelector(function(Rstate) {
        return Rstate.app.theme;
    });
    var chain = useSelector(function(Rstate) {
        return Rstate.app.chain;
    });
    var chainList = useSelector(function(Rstate) {
        return Rstate.app.chainList;
    });
    var dispatch = useDispatch();
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), setLocalState = _useLocalStorageSetState[1];
    var active = useRef(chainList.indexOf(chain) || 0);
    var next = function() {
        var _props_onChainChange;
        var newActive = active.current + 1;
        if (newActive > chainList.length - 1) {
            active.current = 0;
        } else {
            active.current = newActive;
        }
        dispatch.app.SET_CHAIN(chainList[active.current]);
        setLocalState({
            chain: chainList[active.current]
        });
        (_props_onChainChange = props.onChainChange) === null || _props_onChainChange === void 0 ? void 0 : _props_onChainChange.call(props, chainList[active.current]);
    };
    var changeChain = function(newChian) {
        var _props_onChainChange;
        if (newChian === chain) return;
        dispatch.app.SET_CHAIN(newChian);
        setLocalState({
            chain: newChian
        });
        (_props_onChainChange = props.onChainChange) === null || _props_onChainChange === void 0 ? void 0 : _props_onChainChange.call(props, newChian);
    };
    return /*#__PURE__*/ _jsx("div", {
        className: classNames(classes.chain_switch, props.className),
        children: /*#__PURE__*/ _jsx("div", {
            className: classes.chain_container,
            children: chainList.map(function(chainN) {
                return /*#__PURE__*/ _jsx("div", {
                    onClick: function() {
                        changeChain(chainN);
                    },
                    className: classNames(classes.chain_item, chain === chainN ? classes.active_item : ""),
                    children: /*#__PURE__*/ _jsx("span", {
                        children: chainN
                    })
                }, chainN);
            })
        })
    });
}
export default ChainSwitch;

 //# sourceMappingURL=index.js.map